//
//  BroadcasterTableViewCell.m
//  MountEverestFM
//
//  Created by ChenNing on 15/11/5.
//  Copyright © 2015年 chenn. All rights reserved.
//

#import "BroadcasterTableViewCell.h"

@interface BroadcasterTableViewCell()
// 排名
@property(nonatomic,strong)UILabel *indexLabel;
// 曲目图
@property(nonatomic,strong)UIImageView *topImageView;
// 曲目图上的播放图
@property(nonatomic,strong)UIImageView *playImageView;
// 曲目图上的暂停图
@property(nonatomic,strong)UIImageView *pauseImageView;
// 标题
@property(nonatomic,strong)UILabel *titleLabel;
// 作者
@property(nonatomic,strong)UILabel *ownerLabel;
// 集数图标
@property(nonatomic,strong)UIImageView *countImageView;
// 集数
@property(nonatomic,strong)UILabel *countLabel;
@end

@implementation BroadcasterTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.indexLabel = [[UILabel alloc] init];
        self.indexLabel.font = [UIFont systemFontOfSize:15];
        self.indexLabel.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_indexLabel];
        
        self.topImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_topImageView];
        
        self.playImageView = [[UIImageView alloc] init];
        [self.topImageView addSubview:_playImageView];
        
        self.pauseImageView = [[UIImageView alloc] init];
        [self.topImageView addSubview:_pauseImageView];
        
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.font = [UIFont systemFontOfSize:15];
        self.titleLabel.numberOfLines = 0;
        [self.titleLabel sizeToFit];
        [self.contentView addSubview:_titleLabel];
        
        self.ownerLabel = [[UILabel alloc] init];
        self.ownerLabel.font = [UIFont systemFontOfSize:13];
        self.ownerLabel.textColor = [UIColor grayColor];
        [self.contentView addSubview:_ownerLabel];
        
        self.countImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_countImageView];
        
        self.countLabel = [[UILabel alloc] init];
        self.countLabel.font = [UIFont systemFontOfSize:13];
        self.countLabel.textColor = [UIColor grayColor];
        [self.contentView addSubview:_countLabel];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    // 序号
    self.indexLabel.frame = CGRectMake(FM_Screen_Pts_Width*10, 0, FM_Screen_Pts_Width*25, self.contentView.bounds.size.height);
//    self.indexLabel.backgroundColor = [UIColor redColor];
    // 图片
    self.topImageView.frame = CGRectMake(FM_Screen_Pts_Width*40, FM_Screen_Pts_Height*10, FM_Screen_Pts_Width*70, FM_Screen_Pts_Width*70);
    // 播放图标
    self.playImageView.frame = CGRectMake(FM_Screen_Pts_Width*25,FM_Screen_Pts_Width*25, FM_Screen_Pts_Width*20, FM_Screen_Pts_Width*20);
    // 暂停图标
    self.pauseImageView.frame = CGRectMake(FM_Screen_Pts_Width*25,FM_Screen_Pts_Width*25, FM_Screen_Pts_Width*20, FM_Screen_Pts_Width*20);
 
    // 集数图标
    self.countImageView.frame = CGRectMake(FM_Screen_Pts_Width*120,self.topImageView.frame.origin.y+self.topImageView.frame.size.height-FM_Screen_Pts_Height*20, FM_Screen_Pts_Width*20, FM_Screen_Pts_Width*20);
    // 集数
    self.countLabel.frame = CGRectMake(FM_Screen_Pts_Width*145, self.topImageView.frame.origin.y+self.topImageView.frame.size.height-FM_Screen_Pts_Height*20, FM_Screen_Pts_Width*50, FM_Screen_Pts_Width*20);
    // 作者
    self.ownerLabel.frame = CGRectMake(FM_Screen_Pts_Width*120, self.topImageView.frame.origin.y+self.topImageView.frame.size.height-FM_Screen_Pts_Height*20 , FM_Screen_Width-FM_Screen_Pts_Width*130, FM_Screen_Pts_Height*20);
}

- (void)setTopDic:(NSDictionary *)topDic
{
    if (_topDic != topDic) {
        _topDic = topDic;
    }
    [self setTopInfo:topDic];
}

- (void)setIndex:(NSInteger)index
{
    if (_index != index) {
        _index = index;
    }
}
- (void)setTopInfo:(NSDictionary *)topDic
{
    // 序号
    self.indexLabel.text = [NSString stringWithFormat:@"%d",self.index];
    
    // 图片
    NSString *imageUrl = [topDic objectForKey:@"coverSmall"];
    if (imageUrl == nil) {
        imageUrl = [topDic objectForKey:@"coverMiddle"];
    }
    [self.topImageView sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@"find_kind_btn_default@3x.png"]];
    // 播放图标
    self.playImageView.image = [UIImage imageNamed:@"cell_sound_play_n@2x.png"];
    // 暂停图标
    self.pauseImageView.image = [UIImage imageNamed:@"cell_sound_pause_n@2x.png"];
    
    // 标题
    NSString *title = [topDic objectForKey:@"title"];
    CGRect titleRect = [title boundingRectWithSize:CGSizeMake(FM_Screen_Width-FM_Screen_Pts_Width*130, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil];
    CGFloat titleHeight = 0;
    if (titleRect.size.height > FM_Screen_Pts_Width*80/3*2) {
        titleHeight = FM_Screen_Pts_Width*80/3*2;
    }else{
        titleHeight = titleRect.size.height;
    }
    self.titleLabel.frame = CGRectMake(FM_Screen_Pts_Width*120, FM_Screen_Pts_Height*10, FM_Screen_Width-FM_Screen_Pts_Width*130, titleHeight);
    self.titleLabel.text = title;
    
    // 集
    NSString *tracksCount = [[topDic objectForKey:@"tracks"] stringValue];
    if (tracksCount != nil) {
        self.countImageView.image = [UIImage imageNamed:@"album_tracks@3x.png"];
        self.countLabel.text = [NSString stringWithFormat:@"%@ 集",tracksCount];
    }else{
        // 作者
        self.ownerLabel.text = [NSString stringWithFormat:@"by %@",[topDic objectForKey:@"nickname"]];
    }
}

@end
